package com.plian.system.controller.pf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.IAppraisalIndexService;
import com.plian.system.vo.pf.AppraisalIndexVO;
import com.plian.system.wrapper.pf.AppraisalIndexWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @Author: Take-off
 * @Description: TODO 绩效考评指标 控制器
 * @Date: 2022/12/14 9:36 AM
 * @Version: 1.0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("api/appraisalIndex")
@Api(value = "AppraisalIndexController", tags = "绩效考评指标 控制器")
public class AppraisalIndexController extends BaseController {

    private final IAppraisalIndexService appraisalIndexService;

    private final AppraisalIndexWrapper appraisalIndexWrapper;


    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入appraisalIndex")
    public R<AppraisalIndexVO> detail(@RequestBody AppraisalIndex appraisalIndex) {
        AppraisalIndex detail = appraisalIndexService.getOne(Condition.getQueryWrapper(appraisalIndex));
        AppraisalIndexVO appraisalIndexVO = appraisalIndexWrapper.entityToVO(detail);
        return R.data(appraisalIndexVO);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<AppraisalIndexVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();

        QueryWrapper<AppraisalIndex> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new AppraisalIndex()), entityList
        );

        IPage<AppraisalIndex> pages = appraisalIndexService.page(Condition.getPage(query), queryWrapper);
        List<AppraisalIndexVO> appraisalIndexVOS = appraisalIndexWrapper.entityToVO(pages.getRecords());
        ListResult<AppraisalIndexVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(appraisalIndexVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入appraisalIndex")
    @Resubmit(delaySeconds = 0)
    @LogOperation("绩效考评指标-提交")
    public R submit(@Valid @RequestBody AppraisalIndex appraisalIndex) {
        Long id = appraisalIndex.getId();
        Long parentId = appraisalIndex.getParentId();
        if (!Optional.ofNullable(parentId).isPresent()){
            appraisalIndex.setParentId(0L);
        }
        if(Optional.ofNullable(id).isPresent() && id.equals(parentId)){
            return new R(CommonCode.FAIL);
        }
        boolean result = appraisalIndexService.saveOrUpdate(appraisalIndex);
        return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @PostMapping("/remove")
    @LogOperation("绩效考评指标-删除")
    @ApiOperation(value = "删除", notes = "传入 ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return appraisalIndexService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @PostMapping("/listForTree")
    @ApiOperation(value = "树状返回", notes = "传入query")
    public ListResult<AppraisalIndexVO> listForTree(@RequestBody Query query) {
        ListResult<AppraisalIndexVO> result = appraisalIndexService.listForTree(query);
        return result;
    }

}
